vimfandomcom-20200223-history
Replace a word with yanked text
A common requirement, particularly when programming, is to copy or delete text from one location, and use what was copied or deleted to replace text in one or more other locations. This tip describes how this operation is performed using standard Vim techniques, and with some tricks that may make it easier. How to copy/paste For simplicity, this shows how to copy a word and paste it over other words. However, the visual selection could be varied to use any text. Copy a word and paste it over other words: Copy text in quotes, and paste it over other quoted text: Copy a line and paste it over other lines: Deleting, changing and yanking text copies the affected text to the unnamed register (""). Yanking text also copies the text to register 0 ("0). So the command yiw copies the current word to "" and to "0. Typing viw selects the current word, then pressing p pastes the unnamed register over the visual selection. The visual selection that was just replaced is then stored in the default unnamed register. Mapping for paste As seen in the examples above, if you want to paste the same text a second time, you must use the "0 register, as in viw"0p. A workaround is to remap the p command in visual mode so that it first deletes to the black hole register like so: xnoremap p "_dP Note, using P in the remap replicates the default visual mode p function. Feel free to test. And you'd use xnoremap instead of vnoremap, according to the docs. Now you don't have to worry about pasting from the "0 register each subsequent time. However, this remapping prevents you from pasting from other registers in visual mode. So viw"ap to paste from the "a register is now broken. This script may solve that. Alternatively, I guess you could use a leader key instead such as: let mapleader="," xnoremap p "_dP And then the workflow would be: yiw, move, viw,p, move again, viw,p, etc Alternative mapping for paste The mapping xnoremap p "_dP changes the behavior of p when pasting over selected text which is at the end of a line. That occurs because when text at the end of a line is deleted, the cursor moves back to the last character on the line, and P then pastes before that last character. To get a more natural and consistent behavior like the default p, but without overwriting the register we can simply paste and then restore the main register: xnoremap p p:let @"=@0 or xnoremap p p:let @+=@0 when using unnamedplus (:set clipboard=unnamedplus ), or xnoremap p p:let @+=@0:let @"=@0 to restore both unnamed and unnamedplus. Using a text object Instead of using copy/paste, it is often better to use a text object command such as ciw to change the inner word. This method has the advantage of being easily repeatable using the . repeat command. *Use with clipboard @+ *Explain: Use register 2 to insert previously deleted text: diw"2P This process works with any selected text, although it works best if: *The yanked text is characterwise and the replaced text is selected characterwise; or *The yanked text is linewise and the replaced text is selected linewise; or *The yanked text is blockwise and the replaced text is selected blockwise. Typing yiw (yank inner word) is an example of a characterwise copy, while yy (yank current line) is an example of a linewise copy. Stamping With this mapping, you can press S to replace the current word with the last yanked text. You can repeat the operation to replace words at different locations (move to another word and press S, then move to another word and press S, and so on). Each time you press S, the yanked text is "stamped" over the current word. nnoremap S diw"0P The S command is then not available – use the equivalent cc command instead. An alternative mapping uses the black hole register ("_) for the deletion so the unnamed register is not changed: nnoremap S "_diwP The alternative replaces the current word with the last text that was yanked or deleted. This means that you can copy or delete text from one location, then use that text to replace words in multiple other locations. However, if you delete any more text (for example, if you press x to delete an unwanted comma), the ability to easily use the previously-deleted text is lost. In addition to the above, you could use the following to replace visually selected text with the last yanked text. Again, you can quickly repeat this by selecting some more text and pressing S to make the same change. vnoremap S "_d"0P If you want to replace visually selected text with the last yanked or deleted text, use: vnoremap S "_dP Substitute To replace the last searched text with the last yanked text in the entire buffer, use: :%s//\=@"/g See search and replace. From tip 1011 For those who use visual mode, here are some commands and mappings that may be useful: Type gv to reselect the previous visual area. Copy the current word or visually selected text to the clipboard: nnoremap "+yiw vnoremap "+y Replace the current word or visually selected text with the clipboard contents: nnoremap viw"+p vnoremap "+p Prepare a :substitute command using the current word or the selected text: nnoremap yiw:%s/\<"\>/"/gc vnoremap y:%s/\<"\>/"/gc One key does everything. Good for inter-window copying and pasting. "copy vmap "+ygv"zy`> "paste (Shift-F7 to paste after normal cursor, Ctrl-F7 to paste over visual selection) nmap "zgP nmap "zgp imap z vmap "zp`] cmap z "copy register autocmd FocusGained * let @z=@+ See also *Repeat last change Related Plugins * plugin to replace text covered by a motion, entire line(s) or the current selection with the contents of a register; more versatile and covers corner cases better than simple mappings * - Maintains a history of previous yanks, changes and deletes *yankstack - It allows you to yank and delete things without worrying about losing the text that you yanked previously. It effectively turns your default register into a stack, and lets you cycle through the items in the stack after doing a paste. This plugin is intended to be a simpler alternative to the YankRing plugin. *vim-easyclip - This plugin solves that problem by redirecting all change and delete operations to the black hole register and introducing a new operator, 'cut' (by default this is mapped to the m key for 'move'). References * * Comments *Prune out any misguided information. *That will leave some notes on copying a word (or selected text) and pasting it to replace another word (or selected text). *There will also be some notes on visual mode which should be moved elsewhere (possibly 1584). Related: *47 Swapping characters, words and lines *191 Transposing *438 Search and replace in a visual selection *646 Moving lines up or down in a file *1584 Visual selection I hope some of these can be merged, while others will be "see also". Copy/paste tips: *66 Transfer text between two Vim instances *71 Transfer text between two gvim sessions using clipboard *261 Close windows from gvim popup menu *312 Copy, cut and paste *344 Cut or copy lines without counting the lines *356 Quick yank and paste *386 Cut/copy and paste using visual selection *432 Putting the current file on the Windows clipboard *471 Using the mouse for Vim in an xterm *478 Copy the search results into clipboard *829 Copy and paste between Vim instances *834 Word-wise cut, copy and paste *964 GNU/Linux clipboard copy/paste with xclip *984 Accessing the system clipboard *1199 Unconditional linewise or characterwise paste *1268 Copy and paste between sessions using a temporary file *1593 Smart paste *1623 Using the Windows clipboard in Cygwin Vim I'm parking the copy/paste list here; later will work out which is "main" copy/paste tip and will move list there. JohnBeckett 11:24, September 28, 2009 (UTC) :I'd like for this tip to remain about copy/paste as it relates to visual mode, and we should have at least one other (separate) tip on the system clipboard (maybe more than one if specific systems have problems). One more that is a general "how to copy/paste" that also discusses use of registers would be good too. I don't want to see too much combination though, or the tip will probably become too big. --Fritzophrenic 16:27, October 1, 2009 (UTC)